package kuonjisai;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class J01_HelloJDBC {

	// JDBC
	// 데이터베이스에 접근하고 조작하는 것은
	// 아주 중요한 주제중에 하나임
	
	// 데이터베이스에는 여러 브랜드가 있음
	// Oracle, MySQL, MSSQL 등등
	// 모든 데이터베이스서버는 독점적인
	// 방식을 통해 접근하고 관리하도록 허용됨
	// 따라서, 각각의 데이터베이스에 접근하려면
	// 각 브랜드가 제공하는 기술을 사용해야 함
	
	// 운좋게도 자바에서는 서로 다른 데이터베이스에
	// 통일된 방식으로 접근하게 하는 JDBC 를 제공함
	// Java 7은 JDBC 4.1을 제공
	// JDBC Core API : java.sql 패키지
	// JDBC Optional API : javax.sql  패키지	
	
	// JDBC Driver
	// 데이터베이스 제조사가 제공하는 라이브러리 
	// 각 제조사는 자신만의 독점적인 기술을
	// 이 라이브러리에 포함시켜 둠
	// 이것을 JDBC API라는 통일된 코드를 이용해서
	// 데이터베이스를 제어할 수 있음

	// 데이터베이스 접근 5단계
	// 1. 데이터베이스에 접근하기 위해
	//		'JDBC 드라이버' 불러오기
	// 2. 데이터베이스 연결 얻기
	// 3. 실행할 SQL 문장을 만들기 위해
	// 		Statement/ PreparedStatement 객체 생성
	// 4. SQL 문장을 실행하고 데이터베이스로 부터
	// 	 	넘어온 결과를 처리하기 위해
	//		ResultSet 객체를 생성
	// 5. 모든 처리가 끝나면 JDBC 자원 해제
	
	// Oracle JDBC 드라이버 등록
	// ojdbc6.jar 를 라이브러리로 등록
	
	// JDBC 드라이버 불러오기
		// JDBC 드라이버는 데이터베이스에 연결해야 할
		// 필요가 없는 자바 클래스와 jdbc 드라이버 사이의 
		// 계약을 정의하는 인터페이스들을 구현한 것
		// 드라이버는 jar 파일 형태로 배포
		
		// JDBC 드라이버를 불러오는 방법에는
		// 수동 manual과 동적 ynamic 이 있음
		// Class.forName("oracle.jdbc.driver.OracleDriver");
		// Class.forName ( DRV ) ;         -- 변수는 미리 선언해둬야지 편함
		
		// forName 메서드는 ClassNotFoundException
		// 예외를 던지기 때문에 try - catch 블럭안에 둬야함
	
		// 데이터베이스 연결하기
		// Connection 인터페이스는 데이터베이스
		// 연결객체를 위한 그릇(템플릿)임
		// 연결객체를 얻으려면 DriverManager클래스의
		// getConnection 메서드를 이용
		// getConnection 메서드는 앞의 과정에서
		// 메모리에 올려둔 드라이버를 찾고
		// URL, USR, PWD를 통해 데이터베이스 서버에
		// 연결을 하고 그 결과를 Connection으로 반환
	
		// JDBC 연결시 사용하는 URL 은 다음과 같음
		// jdbc:프로토콜:하위이름
		// jdbc:oracle:thin:@주소:포트:DB이름?uesr=아이디&amp;password=비밀번호
		// jdbc:mysql://주소/DB이름?uesr=아이디&amp;password=비밀번호
	
		// 3. SQL 질의문 생성
		// Statement, PreparedStatement 객체는
		// 실행할 SQL 질의문을 의미함
		// createStatement, preparedStatement 메서드를
		// 이용해서 위 객체를 얻을 수 있음
		// 단, 위 메서드를 호출할때는 생성할 SQL 질의문을
		//	인자로 전달해야 함
		// 생성한 객체를 실행하려면 executeQuer나
		// executeUpdate 메서드를 이용함
		// executeQuery : SQL 질의문이 select 인 경우
		// executeUpdate : insert, update, delete 인 경우
		
		// executeUpdate의 실행결과는 질의문에 의해
		// 변경된 레코드 수나 0을 반환
		// executeQuery의 실행결과느느 질의문에 의해
		// 실행된 데이터를 담아두는 ResultSet 객체를 반환
		// 단, 생성된 데이터가 없는 경우 null 은 아님

		// Statement 에서 파생된 PreparedStatement는
		// statement의 인기 있는 대체제임
		// SQL 문을 미리 컴파일하고 저장해두기 때문에
		// 추후 동일한 SQL문을 실행할 경우 
		// 빠르게 처리된다는  장점이 제공
	
		// 4. 결과집합ResultSet 객체 다루기
	    // 결과 집합은 Statement PreparedStatement 에서
		// 반환된 데이터베이스 테이블을 의미함
		// 결과집합 객체는 현재 데이터 행을 가리키는 커서를 가지고 있음
	
		// 커서 처음에는 테이블을 가리키고 있다가
		// next 메서드가 호출되면 다음 레코드로 접근함
		// 이동하고 true 또는 false를 반환함
		// true 가 반환되면 현재 레코드가 존재한다는 의미
		// while을 이용해서 모든 레코드에 접근할 수 있음
	
		// next 메서드로 레코드에 접근한 뒤, 각 필드 값을
		// 가져오려면 getXxx 메서드를 사용해야 함
		// 일반적으로 필드 값을 문자열로 가져오는
		// getString 메서드를 많이 사용
		// 필드값을 int 로 가져오려면 getInt 를 사용하면 됨
	
		// getXxx 메서드에 필드이름 또는 필드 위치값을
		//  인자로 전달해서 필드값을가져올수 있음
		// getString(1)
		// getString( "department_id")
	
		// 5. JDBC 객체 닫기
		// 모든 JDBC 객체는 사용한 후 그들의
		//	 close 메서드를 호출해서 항상 닫아야함
		// try {
		//			..	..		
		//			...	...
		//			rs.close();
		//			pstmt.close();
		//			conn.close();
		//		} catch ( exception ex ) {
	   //				ex.printStackTrace();
		// 	}
		//	  위 코드는 완벽하지 않음
		//		pstmt.close(); 가 실패하는 경우,
		// 그 아래 코드는 실행되지 않기 때문임
		// 따라서, close 관련 코드는 finally 절에 작성해야 함
		// 즉, 예외가 발생하든 하지 않든 간에
		// 무조건 실행되어야 하는 코드는 finally 절에
		// 작성하면프로그램이 다소 완변해 짐 ㅠㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍㅍ.,       mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm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mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
	
	
	public static void main(String[] args) {
		
		// 오라클 데이터베이스 연결정보		
		final String DRV = "oracle.jdbc.driver.OracleDriver";
		final String URL = "jdbc:oracle:thin:@112.216.253.125:1521:xe";
		final String USR = "cjhmasterola";
		final String PWD = "cjhmasterola";

		// MySQL 데이터베이스 연결정보
		// DRV = "com.mysql.jdbc.Driver"
		// URL = "jdbc:mysql://112.216.253.125:3306/zipcode"
		// USR = "zipcode"
		// PWD = "zipcode"
		
		// 데이터베이스 작업용 객체 정의
		 Connection conn = null;					// 연결
		 PreparedStatement pstmt = null;		// 질의
		 ResultSet rs = null;							// 결과
		String SQL = " select * from employees " ;
		 
		 
		//  JDBC 관련코드 작성시 예외처리를 위해
		// try - catch - finally를 작성해둘 것!!
		
		try { 
			// 1. JDBC 드라이버 불러오기
			// Class : 클래스에 대한 객체를 동적으로 생성
			Class.forName(DRV);
			// 2. 데이터베이스 연결
			conn = DriverManager.getConnection(URL, USR, PWD);
			// 3. 실행할 SQL 질의 작성
			pstmt = conn.prepareStatement( SQL );
			// 4a. SQL 질의를 실행하고 결과를 받음
			rs = pstmt.executeQuery();
			// 4b. 받은 결과 처리
			if ( rs.next() ) {						             	 // 결과에 데이터가 존재하면
				
				do{												// 계속 반복해서 그것을 처리
					// rs.getXxx() : ResultSet 객체에서 데이터를 가져옴
					    System.out.print( rs.getString("employee_id") );
						System.out.print( " / " );
					    System.out.println( rs.getString( 4 ) );		// email
				} while ( rs.next() );
				
				
			}
			else {													// 결과에 데이터가 없으면
				System.out.println( "데이터가 없어영~" );
			}
			
			
			} catch ( Exception ex) {
			
				ex.printStackTrace();
				
		} finally {
			// 5. JDBC 객체 해제 ( 역순으로 )
			if ( rs != null ) try {  rs.close(); rs = null; } catch (Exception e) {}
			if ( pstmt != null ) try { pstmt.close(); pstmt = null; } catch (Exception e) {}
			if ( conn != null ) try { conn.close(); conn = null; } catch (Exception e) {}
			
			
			
		}	
					

	}	// main

} // class
